//
//  HJVisitorView.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/16.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit
import SnapKit

protocol HJVisitorViewDelegate: NSObjectProtocol {
    
    func loginBtnClick()
    func registerBtnClick()
}

class HJVisitorView: UIView {
    
    weak var delegate: HJVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func VisitorView(title: String?, imgName: String?)  {
        if let t = title, img = imgName {
            messageLabel.text = t
            houseImageView.image = UIImage(named: img)
        }else{
            startAnimtor()
        }
        
        
        
    }
    func startAnimtor(){
        circleImageView.hidden = false
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 10
        //解决了切换界面动画不执行问题
        anim.removedOnCompletion = false
        circleImageView.layer.addAnimation(anim, forKey: nil)
    }
    
    func setupUI(){
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        addSubview(circleImageView)
        addSubview(coverView)
        addSubview(houseImageView)
        addSubview(messageLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        
        houseImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-30)
        }
//        houseImageView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -30))
        
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-30)
        }
        
//        circleImageView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -30))
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleImageView.snp_bottom).offset(34)
            make.centerX.equalTo(self)
        }
        
//        messageLabel.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 34))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        registBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            make.leading.equalTo(messageLabel)
            make.width.equalTo(100)
        }
        
//        registBtn.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
//        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registBtn)
            make.trailing.equalTo(messageLabel)
            make.width.equalTo(100)
        }
//        
//        loginBtn.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registBtn, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        coverView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleImageView)
            make.centerY.equalTo(circleImageView).offset(-10)
        }
//        coverView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: circleImageView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -10))
        
    }
    
    //懒加载房子图片
    private lazy var houseImageView: UIImageView = {
       let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return imageView
        
    }()
    //懒加载圆圈
    private lazy var circleImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        imageView.hidden = true
        return imageView
    }()
    //懒加载文字
    private lazy var messageLabel: UILabel = {
        let message = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14, preferredMaxLayoutWidth: 230)
        message.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //一下代码已经抽取到Extention
//        message.textColor = UIColor.darkGrayColor()
//        message.font = UIFont.systemFontOfSize(16)
//        message.numberOfLines = 0
//        message.preferredMaxLayoutWidth = 215
        message.textAlignment = NSTextAlignment.Center
        return message
    }()
    
    //注册按钮
    private lazy var registBtn:UIButton = {
       let button = UIButton(btnColor: UIColor.orangeColor(), fontSize: 14)
        button.addTarget(self, action: "ClickRegisterBtn", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        return button
    }()
    
    //登录按钮
    private lazy var loginBtn:UIButton = {
        let button = UIButton(btnColor: UIColor.darkGrayColor(), fontSize: 14)
        button.addTarget(self, action: "ClickLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        return button
    }()
    
    // MARK: - 注册/登录按钮点击事件
    func ClickRegisterBtn(){
        delegate?.registerBtnClick()
    }
    @objc private func ClickLoginBtn(){
        delegate?.loginBtnClick()
    }
    //透明遮挡view
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}
